"""
Case Type   : bugfix
Case Name   : merge into源表和目标表为同一个表，可执行成功
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、建表并插入数据
    2、merge into同一个表
    3、查询merge into后的表
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:建表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fixed_114; " \
                  + f"create table t_fixed_114( product_id integer, product_name varchar2(60), category varchar2(60)); " \
                  + f"insert into t_fixed_114 values (1501, 'vivitar 35mm', 'electrncs'); " \
                  + f"insert into t_fixed_114 values (1502, 'olympus is50', 'electrncs'); " \
                  + f"insert into t_fixed_114 values (1600, 'play gym', 'toys'); " \
                  + f"insert into t_fixed_114 values (1601, 'lamaze', 'toys'); " \
                  + f"insert into t_fixed_114 values (1666, 'harry potter', 'dvd');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        text = '----step2:merge into同一个表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"merge into t_fixed_114 t1 using t_fixed_114 t2 on (t1.product_id = t2.product_id) when matched then " \
                  + f"update set t1.product_name = t2.product_name, t1.category = t2.category where " \
                  + f"t1.product_name != 'play gym' when not matched then insert values (t2.product_id, t2.product_name, " \
                  + f"t2.category) where t2.category = 'books';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.merge_into_success, sql_res, '执行失败' + text)
        self.assertIn('4', sql_res, '执行失败' + text)

        text = '----step3:查询merge into后的表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"select * from t_fixed_114;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn('(5 rows)', sql_res, '执行失败' + text)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fixed_114;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
